//
//  EmojiPictureView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/5/21.
//

import SwiftUI
import Kingfisher

//聊天更多功能
struct ChatMoreBottomView: View {
    @StateObject var viewModel = ChatMoreBottomViewModel()
    let click: (ChatMoreType) -> Void

    var body: some View {
        ZStack{
            GeometryReader { geometry in
                let spacing: CGFloat = 40 // 行间距
                let spacingVertical: CGFloat = 5 //列间距 无效
                let columns = Array(repeating: GridItem(.flexible(), spacing: spacingVertical), count: 4) // 列间距
                // 计算单元格宽度（总宽度减去列间距）
                let itemWidth = (geometry.size.width - spacing * 3) / 4
                ScrollView {
                    LazyVGrid(columns: columns, spacing: spacing) {
                        ForEach(viewModel.items) { bean in
                            VStack(spacing: 10) {
                                Image(bean.resource)
                                    .resizable()
                                    .padding(bean.padding)
                                    .background(Color.hex("#F6F5F5"))
                                    .frame(width: itemWidth, height: itemWidth)
                                    .clipShape(RoundedRectangle(cornerRadius: 15))
                                Text(bean.title)
                                    .font(.system(size: 12))
                                    .foregroundStyle(.bgNormalText4)
                                    .lineLimit(1)
                            }.contentShape(Rectangle())
                                .onTapGesture {
                                    click(bean.type)
                                }
                        }
                        
                    }

                }
            }
        }
        .padding(.horizontal, 20)
        .padding(.vertical, 15)
        .onAppear{
            if !viewModel.isInit{
                viewModel.getData()
                viewModel.isInit = true
            }
        }
    }
}

#Preview {
    ChatMoreBottomView() {_ in}
}
